package com.hlx.service.admin.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlx.entity.admin.Station;
import com.hlx.entity.admin.Train;
import com.hlx.entity.admin.TrainNumber;
import com.hlx.entity.admin.TrainNumberSeat;
import com.hlx.mapper.admin.TrainNumberSeatMapper;
import com.hlx.service.admin.TrainNumberSeatService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Iterator;
import java.util.List;

@Service
public class TrainNumberSeatServiceImpl extends ServiceImpl<TrainNumberSeatMapper, TrainNumberSeat> implements TrainNumberSeatService {
    @Override
    public TrainNumberSeat gerTrainNumberSeatByDateAndTrainNumberId(LocalDate date, Integer trainNumberId) {
        LambdaQueryWrapper<TrainNumberSeat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TrainNumberSeat::getTrainNumberId,trainNumberId);
        queryWrapper.in(TrainNumberSeat::getDate,date);
        return this.getOne(queryWrapper);
    }

    @Override
    public Boolean addTrainNumberSeat(TrainNumber trainNumber, Train train) {
        LocalDate localDate = trainNumber.getStartDate();
        for (Integer i = 0; i < 15; i++){
            TrainNumberSeat trainNumberSeat = new TrainNumberSeat();
            trainNumberSeat.setTrainNumberId(trainNumber.getId());
            trainNumberSeat.setDate(localDate);
            localDate = localDate.plusDays(1);
            trainNumberSeat.setBusiness(train.getTrainBusiness());
            trainNumberSeat.setFirst(train.getTrainFirst());
            trainNumberSeat.setSecond(train.getTrainSecond());
            this.save(trainNumberSeat);
        }
        return true;
    }

    @Override
    public Boolean deleteTrainNumberByTrainNumberId(Integer trainNumberId) {
        LambdaQueryWrapper<TrainNumberSeat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainNumberSeat::getTrainNumberId,trainNumberId);
        return this.remove(queryWrapper);
    }

    @Override
    public Boolean UpdateTrainNumberSeat(Integer id, LocalDate date,Integer seatType) {
        LambdaQueryWrapper<TrainNumberSeat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainNumberSeat::getTrainNumberId,id);
        queryWrapper.eq(TrainNumberSeat::getDate,date);
        TrainNumberSeat trainNumberSeat = this.getOne(queryWrapper);
        if(seatType == 0){
            trainNumberSeat.setBusiness(trainNumberSeat.getBusiness()+1);
        }else if (seatType == 1){
            trainNumberSeat.setFirst(trainNumberSeat.getFirst()+1);
        }else {
            trainNumberSeat.setSecond(trainNumberSeat.getSecond()+1);
        }
        return this.updateById(trainNumberSeat);
    }

}
